
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>留言板V1.0</title>

    <!-- Bootstrap core CSS -->
    <link href="./template/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->

    <!-- Custom styles for this template -->

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">留言板V1.0</a>
        </div>
        <?php if(isset($_SESSION['username'])){ ?>
            <div class="navbar-collapse collapse">
              <div class="navbar-form navbar-right">
               <button type="button" class="btn btn-success"><?php echo $_SESSION['username']; ?></button>
              <a href="./index.php?c=user&a=loginout" class="btn btn-success">退出</a>
              </div>
            </div>
        <?php }else{ ?>
          <div id="navbar" class="navbar-collapse collapse">
            <form class="navbar-form navbar-right" id="login_form">
              <div class="form-group">
                <input type="text" name="username" class="form-control">
              </div>
              <div class="form-group">
                <input type="password" name="password"  class="form-control">
              </div>
              <button type="button" id="login" class="btn btn-success">登录</button>
              <a href="./index.php?c=user&a=reg" class="btn btn-success">注册</a>
            </form>
          </div>
        <?php } ?>
        
      </div>
    </nav>

    <!-- Main jumbotron for a primary marketing message or call to action -->

    <div class="container" style="padding-top: 100px;">
      <!-- Example row of columns -->
      <div class="row" id="main_content">
        <?php foreach ($data as $k => $v) { ?>
        <div class="col-sm-4" >
          <div class="panel panel-primary" >
            <div class="panel-heading">
              <h3 class="panel-title"><?php echo $v['username'] ?>(<?php echo time_format($v['ctime']) ?>)</h3>
            </div>
            <div class="panel-body">
             <?php echo $v['content']; ?>
            </div>
            <div class="panel-body">
              <a href="./index.php?a=edit&id=<?php echo $k; ?>" class="btn btn-success">编辑</a>
              <a href="./index.php?a=del&id=<?php echo $k; ?>" class="btn btn-danger">删除</a>
            </div>
          </div>
        </div>

        <?php } ?>

      </div>
      <div class="row" style="text-align: center;"><button class="btn btn-success" id="more">获取更多</button></div>
      <div class="box" style="margin-top: 50px;">
        <form class="form-inline" action="./index.php?a=add" method="post">
          <div class="form-group">
            <label for="exampleInputName2">姓名</label>
            <input type="text" disabled="" class="form-control" id="exampleInputName2" value="<?php echo isset($_SESSION['username']) ? $_SESSION['username']:''; ?>"  name="username">
          </div>
          <button type="submit" class="btn btn-warning">发布</button>
          <br>
          <textarea name="content" style="margin-top: 10px;width: 100%;" class="form-control" rows="4"></textarea>
        </form>
      </div>
      <hr>

      <footer>
        <p>&copy; 2016 Company, Inc.</p>
      </footer>
    </div>
  </body>
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

  <script>
        var page = 1;
        $('#more').click(function(event) {

            page++;

            $.ajax({
              url: './index.php?a=more',
              type: 'POST',
              dataType: 'html',
              data: {page: page},
            })
            .done(function(res) {
              $('#main_content').append(res)
            })
            .fail(function() {
              console.log("error");
            })
            .always(function() {
              console.log("complete");
            });
            
        });


        $('#login').click(function(event) {

            $.ajax({
              url: './index.php?c=user&a=login',
              type: 'POST',
              dataType: 'json',
              data:$('#login_form').serialize(),
            })
            .done(function(res) {
                if(res.code){
                  alert(res.info);
                }else{
                  location.reload();
                }
            })
            .fail(function() {
              console.log("error");
            })
            .always(function() {
              console.log("complete");
            });
            
        });

  </script>
</html>
